appModule.service("sessionConfigService", [function() {
    var service = {
        StoragesKey: "ssh.session.configs",
        save: function(sshSessionConfig) {
            var sshSessionConfigMap = service.toMap();

            sshSessionConfigMap[sshSessionConfig.name] = sshSessionConfig;
            localStorage.setItem(service.StoragesKey, JSON.stringify(sshSessionConfigMap));
        },
        delete: function(name) {
            var sshSessionConfigMap = service.toMap();
            if(sshSessionConfigMap[name]) {
                sshSessionConfigMap[name] = undefined;
                delete sshSessionConfigMap[name];
                localStorage.setItem(service.StoragesKey, JSON.stringify(sshSessionConfigMap));
                return true;
            }
            return false;
        },
        toMap: function() {
            var sshSessionConfigMap = localStorage.getItem(service.StoragesKey);
            sshSessionConfigMap = (sshSessionConfigMap && JSON.parse(sshSessionConfigMap)) || {};
            return sshSessionConfigMap;
        },
        getAll: function() {
            var sshSessionConfigMap = service.toMap();
            var result = [];
            var name;
            for(name in sshSessionConfigMap) {
                result.push(sshSessionConfigMap[name]);
            }
            return result;
        },
        get: function(name) {
            var sshSessionConfigMap = service.toMap();
            return sshSessionConfigMap[name];
        },
        contains: function(name) {
            return typeof service.get(name) == "object";
        }
    };
    return service;
}]);